


//
//  MBRecoNewsVC.m
//  房源详情-小区详情
//
//  Created by MB__Lee on 2018/5/29.
//  Copyright © 2018年 MB__Lee. All rights reserved.
//

#import "MBRecoNewsVC.h"

#import "MBNewsTableview.h"


#import "MBNewsDetailVC.h"
#import "MBNetTool.h"

#import "FirstNewModel.h"

#import "MBNavigationLeftBar.h"

#import <MJRefresh.h>
#import <MJExtension.h>


@interface MBRecoNewsVC ()

@property(nonatomic,strong)MBNewsTableview *tableview;

@property(nonatomic,assign)NSUInteger page;

@property(nonatomic,strong)MBNavigationLeftBar *bar;
@end


@implementation MBRecoNewsVC

-(MBNewsTableview *)tableview{
    if (!_tableview) {
        
        __weak typeof(self) weakSelf = self;
        MJRefreshNormalHeader *header  =[MJRefreshNormalHeader headerWithRefreshingBlock:^{
            [weakSelf getNewsSourceFromNetWithPage:1];
        }];
        MJRefreshAutoNormalFooter *fooer  = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
            _page +=1;
            
            [weakSelf getNewsSourceFromNetWithPage:_page];
        }];
        
        
        
        _tableview = [[MBNewsTableview alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
        _tableview.newsBlock = ^(NSString *sn) {
            
            MBNewsDetailVC *nsVC = [[MBNewsDetailVC alloc] init];
            nsVC.sn = sn;
            [weakSelf.navigationController pushViewController:nsVC animated:YES];
        };
        
        _tableview.mj_header = header;
        [_tableview.mj_header beginRefreshing];
        _tableview.mj_footer = fooer;
    }
    return  _tableview;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"新闻列表";
    
    
    // Do any additional setup after loading the view.
    [self.view addSubview:self.tableview]; // 添加tableview
    
    [self setReCoNewsVCNaviLeftBar]; // 设置导航栏返回按钮
    
}
-(void)setReCoNewsVCNaviLeftBar{
    _bar = [[MBNavigationLeftBar alloc] init];
    [_bar MBNavigationLeftBar_withVC:self];
    
}
-(void)getNewsSourceFromNetWithPage:(NSUInteger)page{
    
    _page = page;
    
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    dict[@"type"] = @"1";
    dict[@"page"] = [NSString stringWithFormat:@"%zd",page];
    
    [MBNetTool MBPost:News_list paramater:dict success:^(id response) {
        if ([response[@"code"] integerValue] == 10000) {
            NSMutableArray *mut = [FirstNewModel mj_objectArrayWithKeyValuesArray:response[@"data"]];
            
            [self.tableview news_addSource:mut];
            
            [self.tableview.mj_header endRefreshing];
            [self.tableview.mj_footer endRefreshing];
    
        }else if ([response[@"code"] integerValue] == 50000){
            [self.tableview.mj_header endRefreshing];
            [self.tableview.mj_footer endRefreshing];
            
            
        }else{
            [self.tableview.mj_header endRefreshing];
            [self.tableview.mj_footer endRefreshing];
            
        }
        
    } failure:^(NSError *error) {
        [self.tableview.mj_header endRefreshing];
        [self.tableview.mj_footer endRefreshing];
        
    }];
    
    
}




/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
